package org.v2ray.rocket.proxy.support.network

import com.google.gson.JsonSyntaxException
import kotlinx.coroutines.TimeoutCancellationException
import org.v2ray.rocket.proxy.support.R
import org.v2ray.rocket.proxy.support.utils.ResourceUtil
import rxhttp.wrapper.exception.HttpStatusCodeException
import rxhttp.wrapper.exception.ParseException
import java.net.ConnectException
import java.net.SocketTimeoutException
import java.net.UnknownHostException

val Throwable.msg: String
    get() {
        //okhttp全局设置超时
        //协程超时
        return when (this) {
            is UnknownHostException -> { //网络异常
                ResourceUtil.resourceString(R.string.error_unavailable)
            }

            is SocketTimeoutException, is TimeoutCancellationException -> {
                ResourceUtil.resourceString(R.string.error_timeout)
            }

            is ConnectException -> {
                ResourceUtil.resourceString(R.string.error_http)
            }

            is HttpStatusCodeException -> {               //请求失败异常
                ResourceUtil.resourceString(R.string.error_http)
            }

            is JsonSyntaxException -> {  //请求成功，但Json语法异常,导致解析失败
                ResourceUtil.resourceString(R.string.error_parse)
            }

            is ParseException -> {       // ParseException异常表明请求成功，但是数据不正确
                this.message ?: errorCode   //msg为空，显示code
            }

            else -> {
                ResourceUtil.resourceString(R.string.error_http)
            }
        }
    }